package com.qishenyuan.competition.exception;

/**
 * 错误码：xx(模块)-xx(业务)-xx(预留自定义位)
 */
public enum BusinessErrorType {

    // 10开头全局系统异常码
    SYSTEM_ERROR(100000, "System error"),

    // 20-10开头项目组队模块异常码
    IMAGE_FORMAT_ERROR(201001, "Image format error"),
    UPLOAD_FILE_FAILED(201002, "Upload file failed"),
    // 201013错误信息透传给前端
    CREATE_TOPIC_FAILED(201003, "发帖失败，请稍后再试"),

    // 30-10开头用户模块异常码
    CREATE_USER_FAILED(301001, "can not create user now"),
    INVALID_USER_TOKEN(301002, "token is expired"),
    USER_NEED_LOGIN(301003, "The current operation requires user login"),


    ;

    private final int code;
    private final String message;

    BusinessErrorType(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据错误码获取枚举
     */
    public static BusinessErrorType fromCode(int code) {
        for (BusinessErrorType exception : values()) {
            if (exception.code == code) {
                return exception;
            }
        }
        return null;
    }

}
